package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-2-6 11:05
 */
public class 黄金矿工_1219 {
    int result = 0;
    public int getMaximumGold(int[][] grid) {
        int gold = 0;
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] != 0) {
                    dfs(i, j, gold, grid);
                }
            }
        }
        return result;
    }

    private void dfs(int m, int n, int gold, int[][] grid) {
        int old = grid[m][n];
        gold = gold + old;
        result = Math.max(result, gold);
        grid[m][n] = 0;
        int[] dX = {-1, 1, 0, 0};
        int[] dY = {0, 0, -1, 1};
        for (int i = 0; i < 4; i++) {
            int newX = m + dX[i];
            int newY = n + dY[i];
            if (newX >= 0 && newX < grid.length && newY >= 0 && newY < grid[0].length && grid[newX][newY] != 0) {
                dfs(newX, newY, gold, grid);
            }
        }
        grid[m][n] = old;
    }

    @Test
    public void testGetMaximumGold () {
        // int[][] grid = {{1,0,7},{2,0,6},{3,4,5},{0,3,0},{9,0,20}};
        int[][] grid = {{0,6,0},{5,8,7},{0,9,0}};
        System.out.println(getMaximumGold(grid));
    }
}
